package com.zhuoyue.system.service.impl;

import java.util.List;

import com.zhuoyue.common.annotation.DataScope;

import com.zhuoyue.common.utils.StringUtils;
import com.zhuoyue.system.domain.TblDeviceActions;
import com.zhuoyue.system.domain.TblProductInfo;
import com.zhuoyue.system.domain.po.TblDevicePo;
import com.zhuoyue.system.domain.vo.TblDeviceVo;
import com.zhuoyue.system.mapper.ProductInfoMapper;
import com.zhuoyue.system.mapper.TblDeviceActionsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhuoyue.system.mapper.TblDeviceMapper;
import com.zhuoyue.system.service.ITblDeviceService;

/**
 * 设备Service业务层处理
 *
 * @author hewenze
 * @date 2020-08-07
 */
@Service
public class TblDeviceServiceImpl implements ITblDeviceService {
    @Autowired
    private TblDeviceMapper tblDeviceMapper;

    @Autowired
    private TblDeviceActionsMapper actionsMapper;

    @Autowired
    private ProductInfoMapper sysImageMapper;


    /**
     * 查询设备列表
     *
     * @param tblDevicePo 设备
     * @return 设备
     */
    @DataScope(deptAlias = "s_d")
    @Override
    public List<TblDeviceVo> selectTblDeviceList(TblDevicePo tblDevicePo) {
        return tblDeviceMapper.selectTblDeviceList(tblDevicePo);
    }

    @Override
    public List<TblDeviceVo> selectDeviceListByHomeId(TblDevicePo tblDevicePo) {
        List<TblDeviceVo> tblDeviceVos = tblDeviceMapper.selectTblDeviceListByHomeId(tblDevicePo);
        // 查询各个设备的actions
        for (TblDeviceVo tblDeviceVo : tblDeviceVos) {
            List<TblDeviceActions> tblDeviceActions = actionsMapper.listActionsByDeviceId(tblDeviceVo.getId());
            tblDeviceVo.setActions(tblDeviceActions);
        }
        return tblDeviceVos;
    }

    @Override
    public TblDeviceVo selectById(String id) {
        TblDeviceVo deviceVo = tblDeviceMapper.selectDeviceMessageById(id);
        if (deviceVo == null) {
            return null;
        }
        List<TblDeviceActions> tblDeviceActions = actionsMapper.listActionsByDeviceId(id);
        deviceVo.setActions(tblDeviceActions);
        TblProductInfo tblProductInfo = sysImageMapper.selectProdDefaultName(StringUtils.substring(deviceVo.getDeviceType(), 0, 8));
        deviceVo.setDefaultDeviceName(tblProductInfo.getProductName());
        return deviceVo;
    }

    @Override
    public TblDeviceVo selectByMacAndWay(String zigbeeMac, Integer way) {
        TblDeviceVo deviceVo = tblDeviceMapper.selectDeviceByMacAndWay(zigbeeMac, way);
        if (deviceVo == null) {
            return null;
        }
        List<TblDeviceActions> tblDeviceActions = actionsMapper.listActionsByDeviceId(deviceVo.getId());
        deviceVo.setActions(tblDeviceActions);
        TblProductInfo tblProductInfo = sysImageMapper.selectProdDefaultName(StringUtils.substring(deviceVo.getDeviceType(), 0, 8));
        deviceVo.setDefaultDeviceName(tblProductInfo.getProductName());
        return deviceVo;
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public Integer getDeviceCount(TblDevicePo tblDevicePo) {
        return tblDeviceMapper.getDeviceCount(tblDevicePo);
    }
}
